<template lang="html">
  <div class="pagination">
    <ul>
      <li :class="{disabled: curPage == 1}" @click="prevPage" v-if="pageCount > 1">&lt</li>
      <li :class="{active: curPage == 1}" @click="page(1)">1</li>
      <li class="ellipsis" v-show="curPage > 5 && pageCount > 10">...</li>
      <li :class="{active: curPage == index+offset}" v-for="(item,index) in middlePages" @click="page(index+offset)">{{index+offset}}</li>
      <li class="ellipsis" v-show="curPage < bigLimit && pageCount > 10">...</li>
      <li :class="{active: curPage == pageCount}" @click="page(pageCount)" v-if="pageCount > 1">{{pageCount}}</li>
      <li :class="{disabled: curPage == pageCount}" @click="nextPage" v-if="pageCount > 1">&gt</li>
    </ul>
  </div>
</template>

<script>
  export default {
    data(){
      return {
        curPage: 1,
      };
    },
    props:['pageCount'],
    computed:{
      middlePages(){
        if(this.pageCount <= 2){
          return 0;
        }else if(this.pageCount> 2 && this.pageCount <= 10){
          return this.pageCount-2;
        }else{
          return this.curPage > 999 ? 5 : 8;
        }
      },
      bigLimit(){
        return this.middlePages > 5 ? this.pageCount-6 : this.pageCount -3;
      },
      offset(){
        if(this.curPage <= 5){
          return 2;
        }else if(this.curPage >= this.bigLimit){
          return this.bigLimit-2;
        }else{
          return this.middlePages > 5 ? this.curPage-3 : this.curPage-2;
        }
      }
    },
    methods:{
      page(indexPage){
        this.$emit('togglePage',indexPage);
        this.curPage = indexPage;
      },
      prevPage(){
        if(this.curPage != 1){
          this.page(this.curPage-1);
        }
      },
      nextPage(){
        if(this.curPage != this.pageCount){
          this.page(this.curPage+1);
        }
      }
    },
    watch:{
        curPage:{
            handler(val,oldVal){
                this.$emit('pagechange',this.curPage)
            },
            deep:true,
        }
    }
  };
</script>

<style  scoped>
.pagination{
    width: 100%;
    text-align: center;
    position: absolute;
    left: 0;
    bottom: 20px;
}
ul{
    margin: 10px;
}
li{
    cursor: pointer;
    max-width: 80px;
    text-align: center;
    padding: 1px 5px;
    display: inline-block;
    border: 1px solid #e1e1eb;
    margin-right: 5px;
}
li.active{
    background: #4078c0;
    color: #fff;
}
li.ellipsis{
    border: none;
}
li.disabled{
    color: #dcdcdc;
}
</style>